<?php

namespace App\Http\Controllers\Seller;

use App\Http\Requests\Seller\GoodsType\CreateRequest;
use App\Http\Requests\Seller\GoodsType\UpdateRequest;
use App\Models\GoodsType;
use App\Repositories\Seller\GoodsTypeRepository;
use Illuminate\Http\Request;

class GoodsTypeController extends BaseController
{

    //
    public function __construct()
    {
        $this->repo = new GoodsTypeRepository();
    }

    /**
     * @param GoodsType $goods_type
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchInfo(GoodsType $goods_type)
    {
        if ($goods_type->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        return $this->success($goods_type);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchList(Request $request)
    {
        $type = $request->get('type',0);

        $param['parent_id'] = $type;
        $where['seller_id'] = $this->id();
        $page = $this->repo->tree($where, $param);
        return $this->success($page);

    }
    public function fetchSelectTree(Request $request){
        $only_parent = (bool)$request->get('parent');
        $tree = $this->repo->selectTree($this->id(),$only_parent);
        return $this->success($tree);
    }
    /**
     * @param CreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(CreateRequest $request)
    {
        $data = [
            'name' => $request->get('name'),
            'parent_id' => (int)$request->get('parent_id'),
            'status' => $request->get('status'),
            'sort' => $request->get('sort', 1),
            'seller_id' => $this->id(),
            'is_parent'=>$request->get('is_parent',0)
        ];
        $goods_type = $this->repo->create($data);
        return $goods_type ? $this->success($goods_type, '添加成功') : $this->error('添加失败');
    }

    /**
     * @param GoodsType $goods_type
     * @param UpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(GoodsType $goods_type, UpdateRequest $request)
    {
        $data = [
            'name' => $request->get('name'),
            'parent_id' => (int)$request->get('parent_id'),
            'status' => $request->get('status'),
            'sort' => $request->get('sort', 1),
            'is_parent'=>$request->get('is_parent',0)
        ];
        if ($goods_type->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        $goods_type = $this->repo->update($goods_type, $data);
        return $goods_type ? $this->success($goods_type, '更新成功') : $this->error('更新失败');
    }

    /**
     * @param GoodsType $goods_type
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delete(GoodsType $goods_type)
    {
        if ($goods_type->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        if ($goods_type->children()->count()) {
            return $this->error('该分类下还有子分类无法删除');
        }
        if ($goods_type->goods()->count()) {
            return $this->error('该分类下还有商品无法删除');
        }
        return $goods_type->delete() ? $this->success([], '删除成功') : $this->error('删除失败');
    }
    /**
     * @param GoodsType $goods
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus(GoodsType $goodsType){
        if($goodsType->seller_id!=$this->id()){
            return $this->error('记录不存在',[],404,404);
        }
        $data['status'] = !$goodsType->status;
        $this->repo->update($goodsType,$data);
        return $this->success([],'状态修改成功');
    }
}
